package com.tobeface.wechat.request.api;

import com.tobeface.modules.lang.Requires;
import com.tobeface.modules.lang.Strings;
import com.tobeface.wechat.request.AbstractWeChatApiRequestBuilder;
import com.tobeface.wechat.request.WeChatRequest;

/**
 * @author loudyn
 */
public class UserApiRequestBuilder {

    public static final UserApiRequestBuilder INSTANCE = new UserApiRequestBuilder();

    /**
     * @return
     */
    public GetUserInfoApiRequestBuilder get() {
        return new GetUserInfoApiRequestBuilder();
    }

    /**
     * @return
     */
    public GetAllUserInfosApiRequestBuilder getAll() {
        return new GetAllUserInfosApiRequestBuilder();
    }

    private UserApiRequestBuilder() {
    }


    /**
     * @author loudyn
     */
    public class GetUserInfoApiRequestBuilder extends AbstractWeChatApiRequestBuilder {

        private String openId;
        private String lang;

        /**
         *
         */
        GetUserInfoApiRequestBuilder() {
            super("https://api.weixin.qq.com/cgi-bin/user/info", WeChatRequest.Verb.GET);
        }

        /**
         * @param openId
         * @return
         */
        public GetUserInfoApiRequestBuilder withOpenId(String openId) {
            Requires.hasText(openId);
            this.openId = openId;
            return this;
        }

        /**
         * @return
         */
        public GetUserInfoApiRequestBuilder usingCNLang() {
            this.lang = "zh_CN";
            return this;
        }

        /**
         * @return
         */
        public GetUserInfoApiRequestBuilder usingTWlang() {
            this.lang = "zh_TW";
            return this;
        }

        /**
         * @return
         */
        public GetUserInfoApiRequestBuilder usingENLang() {
            this.lang = "en";
            return this;
        }

        @Override
        protected void configRequest(WeChatRequest request) {
            request.addQueryParam("openid", openId).addQueryParam("lang", lang);
        }
    }


    /**
     * @author loudyn
     */
    public class GetAllUserInfosApiRequestBuilder extends AbstractWeChatApiRequestBuilder {

        private String nextOpenId;

        /**
         *
         */
        GetAllUserInfosApiRequestBuilder() {
            super("https://api.weixin.qq.com/cgi-bin/user/get", WeChatRequest.Verb.GET);
        }

        /**
         * @param nextOpenId
         * @return
         */
        public GetAllUserInfosApiRequestBuilder afterOpenId(String nextOpenId) {
            Requires.hasText(nextOpenId);
            this.nextOpenId = nextOpenId;
            return this;
        }

        @Override
        protected void configRequest(WeChatRequest request) {
            if (Strings.isBlank(nextOpenId)) {
                return;
            }
            request.addQueryParam("next_openid", nextOpenId);
        }
    }
}
